package com.young.fs.web;

import com.young.common.util.CommonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.common.util.Page;
import com.young.fs.model.FileInfo;
import com.young.fs.service.IFileInfoService;
import com.young.fs.util.FsConstants;
import com.young.interfaces.log.annotation.Log;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 文件信息请求处理Controller
 * yangwei
 */
@Controller
@RequestMapping(value = "/fs/fileInfo", method = {RequestMethod.GET, RequestMethod.POST})
public class FsFileInfoController {

    @Resource(name="fsFileInfoService")
    IFileInfoService fileInfoService;

    /**
     * 获取文件信息列表
     * @param fileInfo
     * @param request
     * @return
     */
    @Log("查询文件信息列表")
    @RequestMapping("/getFileInfoList")
    @ResponseBody
    public Object getFileInfoList(FileInfo fileInfo, HttpServletRequest request) {
        List<FileInfo> list = fileInfoService.getFileInfoList(fileInfo);
        return Result.buildSuccess(list);
    }

    /**
     * 获取文件夹下的文件信息,包含文件夹
     * @param fileInfo
     * @param request
     * @return
     */
    @Log("查询文件夹下的文件信息(含文件夹)")
    @RequestMapping("/getFileInfoByFolder")
    @ResponseBody
    public Object getFileInfoByFolder(FileInfo fileInfo, HttpServletRequest request) {
        List<FileInfo> list = fileInfoService.getFileInfoByFolder(fileInfo.getFolderId());
        return Result.buildSuccess(list);
    }

    /**
     * 获取文件信息列表(分页)
     * @param fileInfo
     * @param request
     * @return
     */
    @Log("分页查询文件信息列表")
    @RequestMapping("/getFileInfoPage")
    @ResponseBody
    public Object getFileInfoPage(FileInfo fileInfo, HttpServletRequest request) {
        Page page = CommonUtil.getPageFromRequest(request);
        List<FileInfo> list = fileInfoService.getFileInfoPage(fileInfo, page);
        page.setData(list);
        return page;
    }

    /**
     * 添加文件信息
     * @param fileInfo
     * @param request
     * @return
     */
    @Log("添加文件信息")
    @RequestMapping("/insertFileInfo")
    @ResponseBody
    public Object insertFileInfo(FileInfo fileInfo, HttpServletRequest request) {
        int num = fileInfoService.insertFileInfo(fileInfo);
        if(num < 0){
            return Result.buildFail("添加文件信息失败!");
        }
        return Result.buildSuccess();
    }

    /**
     * 修改文件信息
     * @param fileInfo
     * @param request
     * @return
     */
    @Log("修改文件信息")
    @RequestMapping("/updateFileInfo")
    @ResponseBody
    public Object updateFileInfo(FileInfo fileInfo, HttpServletRequest request) {
        int num = fileInfoService.updateFileInfo(fileInfo);
        if(num < 0){
            return Result.buildFail("修改文件信息失败!");
        }
        return Result.buildSuccess();
    }

    /**
     * 删除文件信息
     * @param request
     * @return
     */
    @Log("删除文件信息")
    @RequestMapping("/deleteFileInfo")
    @ResponseBody
    public Object deleteFileInfo(HttpServletRequest request) {
        Map<String,Object> parameter = CommonUtil.getParameterFromRequest(request);
        String idsStr = parameter.get("ids")+"";
        String[] idArr;
        if(StringUtils.isNotBlank(idsStr)) {
            idArr = idsStr.split(",");
            int num = fileInfoService.deleteFileInfo(idArr);
            if(num < 0){
                return Result.buildFail("删除文件信息失败!");
            }
        }else{
            return Result.buildFail("删除失败:待删除文件ID无效!");
        }

        return Result.buildSuccess();
    }

    /**
     * 获取文件夹树
     * @param fileInfo
     * @param request
     * @return
     */
    @Log("获取文件夹树")
    @RequestMapping("/getFolderTree")
    @ResponseBody
    public Object getFolderTree(FileInfo fileInfo, HttpServletRequest request) {
        List<FileInfo> list = fileInfoService.getFolderTree(fileInfo);
        return Result.buildSuccess(list);
    }

    /**
     * 获取文件类型列表
     * @param request
     * @return
     */
    @Log("查询文件类型枚举项列表")
    @RequestMapping("/getFileTypeList")
    @ResponseBody
    public Object getFileTypeList(HttpServletRequest request) {
        /*List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Class cls = EFileType.class;
        Field[] fields = cls.getDeclaredFields();
        for(Field field : fields){
            Map<String,String> map = new HashMap<String, String>();
            EFileType.Remark remark = field.getAnnotation(EFileType.Remark.class);
            if(remark != null){
                map.put("key", field.getName());
                map.put("value", remark.value());
                list.add(map);
            }
        }*/
        return Result.buildSuccess(fileInfoService.getFileTypeList());
    }

    /**
     * 统计文件数量
     * @param request
     * @return
     */
    @Log("文件数量统计")
    @RequestMapping("/statisFileNum")
    @ResponseBody
    public Object statisFileNum(FileInfo fileInfo, HttpServletRequest request) {

        return Result.buildSuccess(fileInfoService.statisFileNum(fileInfo));
    }

    /**
     * 获取一级目录,即ROOT下的直接子目录
     * @param request
     * @return
     */
    @Log("查询一级目录")
    @RequestMapping("/getOneLevelFolder")
    @ResponseBody
    public Object getOneLevelFolder(HttpServletRequest request) {
        List<FileInfo> list = fileInfoService.getSubFolderList(FsConstants.ROOT_FOLDER_ID);
        if (list != null && list.size() > 0){
            for (FileInfo fileInfo : list){
                //查询子文件数量
                fileInfo.setChildFileNum(fileInfoService.countFileNumByFolderId(fileInfo.getId()));
                if (fileInfo.getAddr() != null){
                    File file = new File(fileInfo.getAddr());
                    if (file.exists() && file.isDirectory()){
                        //设置文件夹大小
                        fileInfo.setSize((int)(FileUtils.sizeOfDirectory(file) / 1024));
                    }
                }
            }
        }
        return Result.buildSuccess(list);
    }

    /**
     * 查询根目录信息
     * @param request
     * @return
     */
    @Log("查询根目录信息")
    @RequestMapping("/getRootFolderInfo")
    @ResponseBody
    public Object getRootFolderInfo(HttpServletRequest request) {
        FileInfo fileInfo = fileInfoService.getFolderInfo(FsConstants.ROOT_FOLDER_ID);
        if (fileInfo != null){
            //根目录下的文件信息(含文件夹)
            List<FileInfo> list = fileInfoService.getFileInfoByFolder(FsConstants.ROOT_FOLDER_ID);
            //剔除文件夹并计算文件大小
            if (list != null && list.size() > 0){
                /*Iterator<FileInfo> it = list.iterator();
                int size = 0;
                while (it.hasNext()){
                    FileInfo one = it.next();
                    if (one != null && one.isFile()){
                        size += one.getSize();
                    }
                }*/
                fileInfo.setChildFileNum(list.size());
                File file = new File(fileInfo.getAddr());
                if (file.exists() && file.isDirectory()){
                    //设置文件夹大小
                    fileInfo.setSize((int)(FileUtils.sizeOfDirectory(file) / 1024));
                }
            }
        }
        return Result.buildSuccess(fileInfo);
    }

    /**
     * 根据业务编号查询文件信息
     * @param businessCode 业务编号
     * @param request
     * @return
     */
    @Log("根据业务编号查询文件信息")
    @RequestMapping("/getFileInfoListByBusinessCode")
    @ResponseBody
    public Object getFileInfoListByBusinessCode(String businessCode, HttpServletRequest request) {
        if (StringUtils.isBlank(businessCode)){
            return Result.buildFail("业务编号无效");
        }
        return Result.buildSuccess(fileInfoService.getFileInfoListByBusinessCode(businessCode));
    }

}
